package project.dade.cn.bst.utils;/**
 * © 2015 zhoudada
 * 名称：Tdzt
 * 描述：网络请求异常统一处理类
 *
 * @author周大大
 * @version v1.0
 * @date：2015/9/11
 */

import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;

import project.dade.cn.bst.model.BaseRequestModel;
import project.dade.cn.bst.model.BaseRequestsModel;


/**
 * User: zhoudada
 * Date: 2015-06-27
 * Time: 18:26
 * FIXME
 */
public class NetRequestUtil {

    /**
     * 请求成功异常处理
     *
     * @param response    请求数据
     * @param requestDesc 请求的描述
     * @return
     */
    public static String requestSuccessError(String response, String requestDesc) {

        if (!TextUtils.isEmpty(response)) {
            //判断JSON异常
            if (JsonUtil.getJSONType(response) == JsonUtil.JSON_TYPE.JSON_TYPE_OBJECT) {
                try {
                    BaseRequestModel model = JSON.parseObject(response, BaseRequestModel.class);
                    if (model != null && model.getResult() == 0) {
                        //获取请求成功
                        return "";
                    } else {
                        //获取请求失败
                        if (!TextUtils.isEmpty(model.getMessage()))
                            return requestDesc + "失败：" + model.getMessage();
                    }
                } catch (Exception e) {
                    return requestDesc + "失败：服务器数据异常";
                }

            } else {

                return requestDesc + "失败：服务器数据异常";
            }

        } else {
            return requestDesc + "失败：服务器请求异常";
        }
        return requestDesc + "失败：请求异常";

    }

    /**
     * 请求成功异常处理
     *
     * @param response    请求数据
     * @param requestDesc 请求的描述
     * @return
     */
    public static String requestsError(String response, String requestDesc) {

        if (!TextUtils.isEmpty(response)) {
            //判断JSON异常
            if (JsonUtil.getJSONType(response) == JsonUtil.JSON_TYPE.JSON_TYPE_OBJECT) try {
                BaseRequestsModel model = JSON.parseObject(response, BaseRequestsModel.class);
                if (model != null && model.getResult() == 0 ) {
                    //获取请求成功
                    return "";

                } else {

                    Log.e("TAG", model.getMessage() + "-----------------------" + model.getResult() + "----------" + model.getData());
                    if (!TextUtils.isEmpty(model.getMessage()))
                        return requestDesc + "失败：" + model.getMessage();
                }
            } catch (Exception e) {
                return requestDesc + "失败：服务器数据异常";
            }
            else {

                return requestDesc + "失败：服务器数据异常";
            }

        } else {
            return requestDesc + "失败：服务器请求异常";
        }
        return requestDesc + "失败：请求异常";

    }
}
